from __init__ import BaseResponse, log, ReturnResponse
from fastapi import APIRouter
from utils.mysql import MysqlDbUtil

router = APIRouter()


@router.get("/test", response_model=BaseResponse)
def test():
    log.info("success", extra={"endpoint": "/test"})
    return ReturnResponse(data=[{"test": "11"},{"test": "22"}])


@router.get("/test_items/{item_id}", response_model=BaseResponse)   
def test(item_id: int):
    log.info("success", extra={"endpoint": "/test_items"})
    return ReturnResponse(data={"test": item_id})


@router.get("/test_sql", response_model=BaseResponse)
def test_query():
    try:
        db = MysqlDbUtil("dev")
        res = db.query(f"select * from auth_test.users ")
        log.info("success", extra={"endpoint": "/test_query"})
        return {"code": 200, "message": "success", "data": {"users": res.to_dict(orient='records')}}
    except Exception as e:
        log.error(f"{str(e)}", extra={"endpoint": "/test_query"})
        return {"code": 500, "message": repr(e), "data": {"users": []}}